<template>
  <div class="app-container">
    <el-card class="operate-container" shadow="never">
      <i class="el-icon-tickets" style="margin-top: 5px"></i>
      <span style="margin-top: 5px">数据列表</span>
      <el-button
        v-if="isAuth('ums:permission:create')"
        class="btn-add"
        @click="handleAddMenu()"
        size="mini">
        添加
      </el-button>
    </el-card>
    <div class="table-container">
      <el-table ref="menuTable"
                style="width: 100%"
                :data="list"
                v-loading="listLoading" border>

        <table-tree-column
          prop="name"
          header-align="center"
          treeKey="id"
          label="菜单名称">
        </table-tree-column>
        <el-table-column
          prop="value"
          header-align="center"
          align="center"
          :show-overflow-tooltip="true"
          label="授权值">
        </el-table-column>
        <el-table-column
          prop="uri"
          header-align="center"
          align="center"
          :show-overflow-tooltip="true"
          label="前端资源路径">
        </el-table-column>
        <el-table-column
          prop="type"
          header-align="center"
          align="center"
          label="类型">
          <template slot-scope="scope">
            <el-tag v-if="scope.row.type === 0" size="small">目录</el-tag>
            <el-tag v-else-if="scope.row.type === 1" size="small" type="success">菜单</el-tag>
            <el-tag v-else-if="scope.row.type === 2" size="small" type="info">按钮</el-tag>
          </template>
        </el-table-column>
        <el-table-column label="启用状态" width="100" align="center">
          <template slot-scope="scope">
            <el-switch
              @change="handleHiddenChange(scope.$index, scope.row)"
              :active-value="1"
              :inactive-value="0"
              v-model="scope.row.status">
            </el-switch>
          </template>
        </el-table-column>
        <el-table-column label="排序" width="100" align="center">
          <template slot-scope="scope">{{scope.row.sort }}</template>
        </el-table-column>
        <el-table-column label="操作" width="200" align="center">
          <template slot-scope="scope">
            <el-button
              v-if="isAuth('ums:permission:update')"
              size="mini"
              type="text"
              @click="handleUpdate(scope.$index, scope.row)">编辑
            </el-button>
            <el-button
              v-if="isAuth('ums:permission:delete')"
              size="mini"
              type="text"
              @click="handleDelete(scope.$index, scope.row)">删除
            </el-button>
          </template>
        </el-table-column>
      </el-table>
      <!--      弹窗 新增或编辑-->
      <addOrUpdate v-if="addOrUpdateVisible" ref="addOrUpdate" @refreshDataList="getList"></addOrUpdate>
    </div>
  </div>
</template>

<script>
  import {fetchList,deleteMenu,updateHidden} from '@/api/ums/menu'
  import TableTreeColumn from '@/components/table-tree-column';
  import {translateTreeData} from '@/utils/index';
  import addOrUpdate from './components/menu-add-or-update'


  export default {
    name: "menuList",
    data() {
      return {
        list: null,
        listLoading: true,
        addOrUpdateVisible: false
      }
    },
    created() {
      this.getList();
    },
    components: {
      TableTreeColumn,
      addOrUpdate
    },
    methods: {
      handleAddMenu(id) {
        this.addOrUpdateVisible = true;
        this.$nextTick(() => {
          this.$refs.addOrUpdate.init(id)
        })
      },
      getList() {
        this.listLoading = true;
        fetchList().then(response => {
          this.list = translateTreeData(response.data, 'id','pid');
          this.listLoading = false;
        });
      },
      handleHiddenChange(index, row) {
        updateHidden(row.id,{status:row.status}).then(response=>{
          this.$message({
            message: '修改成功',
            type: 'success',
            duration: 1000
          });
        });
      },
      handleUpdate(index, row) {
        this.addOrUpdateVisible = true;
        this.$nextTick(() => {
          this.$refs.addOrUpdate.init(row.id)
        })
      },
      handleDelete(index, row) {
        this.$confirm('是否要删除该菜单', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          deleteMenu(row.id).then(response => {
            this.$message({
              message: '删除成功',
              type: 'success',
              duration: 1000
            });
            this.getList();
          });
        });
      }
    }
  }
</script>

<style scoped>

</style>
